<template>
  <div class="box">
    <Card>
      <span slot="title">添加账号</span>
      <div slot="title1">
        <el-form
          :model="ruleForm"
          status-icon
          :rules="rules"
          ref="ruleForm"
          label-width="100px"
          class="demo-ruleForm"
        >
          <el-form-item label="账号" prop="pass">
            <el-input
              status-icon
              type="text"
              v-model="ruleForm.pass"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="pwd">
            <el-input
              status-icon
              type="password"
              v-model="ruleForm.pwd"
              autocomplete="off"
            ></el-input>
          </el-form-item>

          <el-form-item label="用户组" prop="user">
            <el-select v-model="ruleForm.region">
              <el-option label="普通管理员" value="普通管理员"></el-option>
              <el-option label="超级管理员" value="超级管理员"></el-option>
            </el-select>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" @click="submitForm()">提交</el-button>
            <el-button @click="resetForm()">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </Card>
  </div>
</template>

<script>
//引入post
import { apiuser_js } from "@/utils/api.js";
import local from "@/utils/sess.js"
//引入axios

import Card from "@/components/Card.vue";
import Add from "@/utils/ruler.js";
export default {
  components: {
    Card,
    Add,
  },
  data() {
    //在data之前来验证
    let proPass = (rulers, value, callback) => {
      //判断它的值 不能为空
      if (!value) {
        callback(new Error("内容不能为空"));
      } else if (!Add.pass.test(value)) {
        //密码强度正则，最少6位，包括至少1个大写字母，1个小写字母，1个数字，1个特殊字符
        callback(new Error("用户名正则，4到16位（字母，数字，下划线，减号）"));
      } else {
        callback();
      }
    };
    //验证密码
    let proPwd = (rulers, value, callback) => {
      if (!value) {
        callback(new Error("内容不能为空"));
      } else if (!Add.pwd.test(value)) {
        callback(
          new Error(
            "最少6位，包括至少1个大写字母，1个小写字母，1个数字，1个特殊字符"
          )
        );
      } else {
        callback();
      }
    };
    //引入组件
    return {
      //表单之王双向绑定
      ruleForm: {
        pass: "",
        pwd: "",
        region: "",
      },
      //验证规则
      rules: {
        //vlidator  自定义正则规则验证
        //账户的验证
        pass: { validator: proPass, trigger: "blur" },
        pwd: { validator: proPwd, trigger: "blur" },
      },
    };
  },
  methods: {
    //提交和重置按钮
    async submitForm() {
      let pamars = {
        account: this.ruleForm.pass,
        password: this.ruleForm.pwd,
        userGroup: this.ruleForm.region,
      };

      //axois配置
      let{code,msg} = await apiuser_js(pamars);

      if (code == 0) {

        //设置安全拦截
        // http request拦截器 添加一个请求拦截器
       
      }
      //提交的时候要同时验证这两个表单的数据

      this.$refs.ruleForm.validate((vili) => {
        console.log(vili);
      });
    },

    resetForm() {
      this.$refs.ruleForm.resetFields();
    },
  },
};
</script>

<style lang="less" scoped>
.box {
  padding: 20px;
  box-sizing: border-box;
  background: #ffffff;
}

/deep/.el-input__inner {
  width: 200px;
}
</style>